package org.mzy.org.service.impl;

import org.mzy.basic.service.impl.BaseServiceImpl;
import org.mzy.basic.util.MD5Utils;
import org.mzy.basic.util.StrUtils;
import org.mzy.org.domain.Employee;
import org.mzy.org.domain.Shop;
import org.mzy.org.mapper.EmployeeMapper;
import org.mzy.org.mapper.ShopMapper;
import org.mzy.org.service.IEmployeeService;
import org.mzy.user.domain.Logininfo;
import org.mzy.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * @Package: org.mzy.org.service.impl
 * @ClassName: EmployeeServiceImpl
 * @Date:2022/10/9 18:05
 * @Description:
 */
@Service
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private LogininfoMapper logininfoMapper;
    @Autowired
    private ShopMapper shopMapper;

    @Override
    public void add(Employee employee) {
        // 加密
        Employee employee1 = this.dtoConvertEmp(employee);
        // 将Employee类型数据复制到Logininfo类型logininfo
        Logininfo logininfo = this.empConvertLogininfo(employee1);
        // 将Employee类型数据复制到Shop类型shop
        Shop shop = this.empConvertShop(employee1);
        // 新增登陆表数据
        logininfoMapper.add(logininfo);
        // 新增店铺表数据
        shopMapper.add(shop);
        // 登录id复制给员工的登录id
        employee1.setLogininfoId(logininfo.getId());
        // 店铺id复制给员工的店铺id
        employee1.setShopId(shop.getId());
        // 调用添加方法
        employeeMapper.add(employee1);
    }

    @Override
    public void update(Employee employee) {
        Employee employee2 = this.dtoConvertEmp(employee);
        Logininfo logininfo = this.empConvertLogininfo(employee2);
        logininfo.setId(employee2.getLogininfoId());
        logininfoMapper.update(logininfo);
        employee2.setLogininfoId(logininfo.getId());
        employeeMapper.update(employee2);
    }

    private Employee dtoConvertEmp(Employee employee) {
        // 盐值
        String salt = StrUtils.getComplexRandomString(32);
        // 密码
        String password = salt + employee.getPassword();
        // 加密
        String encryPwd = MD5Utils.encrypByMd5(password);
        return Employee
                .builder()
                .id(employee.getId())
                .username(employee.getUsername())
                .phone(employee.getPhone())
                .email(employee.getEmail())
                .salt(salt)
                .password(encryPwd)
                .departmentId(employee.getDepartmentId())
                .logininfoId(employee.getLogininfoId())
                .build();
    }

    private Logininfo empConvertLogininfo(Employee employee) {
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee, logininfo);
        return logininfo;
    }

    private Shop empConvertShop(Employee employee) {
        Shop shop = new Shop();
        BeanUtils.copyProperties(employee, shop);
        return shop;
    }

    @Override
    public Employee selectByLogininfoId(Long id) {
        return employeeMapper.selectByLogininfoId(id);
    }


    @Override
    public List<Employee> findByShopId(Long shopId) {
        return employeeMapper.findByShopId(shopId);

    }
}
